<?php
error_reporting(E_ALL);
set_time_limit(0);
require_once './PHPExcel.php';

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'OPENID')
            ->setCellValue('B1', '姓名')
            ->setCellValue('C1', '手机号')
            ->setCellValue('D1', '地址')
            ->setCellValue('E1', '意向家电品类')
            ->setCellValue('F1', '是否需要上门收旧家电')
            ->setCellValue('G1', '参与时间')
            ->setCellValue('H1', '渠道')
;
 
            
include '../../c/config.php';
$day=date("Y-m-d"); 
$qudaoList=array(
    1 =>'海信聚好看开机画面',
    2 =>'海信在线公众号',
    3 =>'朋友圈海报',
    4 => '朋友圈H5广告',
    'share'=>'朋友圈分享'
);

$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
 
$sql="SELECT * FROM `userinfo` ";
$query =$mysqli->query($sql);
$index=2;
while ($list=$query->fetch_array()){
    $qudao='公众号子菜单';
    $getU=$mysqli->query("SELECT lai FROM `users` where openid='".$list['openid']."' limit 0,1 ");
        if($getU->num_rows>0){
            $getData=$getU->fetch_array();
            if (isset($qudaoList[$getData['lai']])){
                $qudao=$qudaoList[$getData['lai']];
            }
        }  
        $ishui=trim($list['ishui']);
        if ('yes' == $ishui){
            $shifou='是';
        }else{
            $shifou='否';
        }
    
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$index, $list['openid'])
    ->setCellValue('B'.$index, $list['name'])
    ->setCellValue('C'.$index, $list['phone'])
    ->setCellValue('D'.$index, $list['sheng'].' '.$list['shi'])
    ->setCellValue('E'.$index, $list['jia'])
    ->setCellValue('F'.$index, $shifou)
    ->setCellValue('G'.$index, $list['createtime'])
    ->setCellValue('H'.$index, $qudao)
    ;
    $index++;
     
    
}
 
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('用户数据');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户数据'.$day.'.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
